//
//  XKBankCardManageController.m
//  XKJY
//
//  Created by xuyaguang on 2017/1/13.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKBankCardManageController.h"
#import "XKAddBankCardController.h"
#import "XKMineBankCarCell.h"
#import "XKBankListModel.h"
#import "XKBankDataModel.h"
#import "XKMineCardTipView.h"
static NSString *const XKMineBankCarCellID = @"XKMineBankCarCellReuseIdentifier";

@interface XKBankCardManageController ()
@property (nonatomic, strong) NSMutableArray *cardModelArrayM;
@property (nonatomic, strong) UIButton *addButton;
@property (nonatomic, strong) UIButton *deleteButton;
@property (nonatomic, strong) UIBarButtonItem *addItemButton;
@property (nonatomic, strong) UIBarButtonItem *editItemButton;

@property (nonatomic, strong) XKMineCardTipView *tipView;
@property (nonatomic, assign, getter = isTipShow) BOOL tipShow;
@end

@implementation XKBankCardManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationItemBtn];
    [self setTableView];
    
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self getUserBankCardList];
}

#pragma mark - 设置导航栏添加银行卡按钮
- (void)setNavigationItemBtn {
    _addItemButton = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addBankCardItemBtnClick:)];
    self.navigationItem.rightBarButtonItems = @[_addItemButton];
}

#pragma mark - 添加银行卡跳转页
- (void)addBankCardItemBtnClick:(UIButton *)sender {
    
    XKAddBankCardController *addBankCardVC = [XKAddBankCardController new];
    [self.navigationController pushViewController:addBankCardVC animated:YES];
}


#pragma mark - 设置TableVIew
- (void)setTableView {
    
    self.navigationItem.title = @"银行卡管理";
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self getUserBankCardList];
    }];
    
    UIView *footerView = [UIView new];
    footerView.backgroundColor = kBaseGrayColor;
    footerView.frame = CGRectMake(0, 0, Main_Width, 44*Scale_W);
    self.tableView.tableFooterView = footerView;
}

#pragma mark - 获取银行卡列表
- (void)getUserBankCardList {
    @weakify(self);
    [APIManager POSTRequestWith:APIPOSTRequestState_BankCardList parameters:@{} successBlock:^(NSDictionary *successDict) {
        @strongify(self);
        [self.tableView.mj_header endRefreshing];
        NSLog(@"%@",[NSString jsonToString:successDict]);
        
        XKBankListModel *cardModel = [XKBankListModel mj_objectWithKeyValues:successDict];
        self.cardModelArrayM = cardModel.data.mutableCopy;
        if (cardModel.data.count > 0) {
            [self dismissTipView];
            [self.tableView reloadData];
        } else {
            [self showTipView];
        }
    } failureBlock:^(NSDictionary *failureDict) {
        [self.tableView.mj_header endRefreshing];
    } errorBlock:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

// 显示提示窗
- (void)showTipView {
    if (self.tipShow) {
        return;
    }
    _tipView = [XKMineCardTipView new];
    _tipView.frame = CGRectMake(0, -44, Main_Width, 60*Scale_W);
    _tipView.alpha = 0;
    [self.view addSubview:_tipView];
    @weakify(self);
    [UIView animateWithDuration:0.3 delay:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        @strongify(self);
        self.tipShow = YES;
        self.tipView.alpha = 1;
        self.tipView.frame = CGRectMake(0, 0, Main_Width, 60*Scale_W);
        self.tipView.transform = CGAffineTransformTranslate(self.tipView.transform, 0, 0);
    } completion:^(BOOL finished) {
        
    }];
}

// 移除提示窗
- (void)dismissTipView {
    [self.tipView removeFromSuperview];
    self.tipShow = NO;
}

#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.cardModelArrayM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XKMineBankCarCell *cell = [tableView dequeueReusableCellWithIdentifier:XKMineBankCarCellID];
    if (cell == nil) {
        cell = [[XKMineBankCarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XKMineBankCarCellID];
    }
    cell.bankCardDataModel = self.cardModelArrayM[indexPath.section];
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XKBankDataModel *model = self.cardModelArrayM[indexPath.section];
    @weakify(self);
    [APIManager POSTRequestWith:APIPOSTRequestState_BankCardDelete parameters:@{@"bank_id":model.card_id, @"bank_cardnum":model.cardnum} successBlock:^(NSDictionary *successDict) {
        NSLog(@"银行卡删除成功:%@",[NSString jsonToString:successDict]);
        @strongify(self);
        [SVProgressHUD showSuccessWithStatus:successDict[@"msg"]];
        [self.cardModelArrayM removeObjectAtIndex:indexPath.section];
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
    } failureBlock:^(NSDictionary *failureDict) {
        
    } errorBlock:^(NSError *error) {
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    return headerView;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 8;
//}



#pragma mark - 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([self.delegate respondsToSelector:@selector(advanceMoneyWithBankModel:)]) {
        [self.delegate advanceMoneyWithBankModel:self.cardModelArrayM[indexPath.section]];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < 0) {
        [self dismissTipView];
    }
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
